<?php

use Illuminate\Database\Seeder;
use Yangyifan\Library\UtilityLibrary;

class MenusTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        echo "Start: " . __CLASS__ . " \r\n";

        $menus = [
            ['name' => '基本', 'url' => '', 'icon' => 'video-camera', 'children' => [
                ['name' => '排版', 'url' => '/home/typo', 'icon' => 'video-camera'],
                ['name' => 'Icon', 'url' => '/home/icons', 'icon' => 'video-camera'],
                ['name' => '按钮', 'url' => '/home/buttons', 'icon' => 'video-camera'],
                ['name' => '文本&角标', 'url' => '/home/labels-badges', 'icon' => 'video-camera'],
                ['name' => '丝带', 'url' => '/home/ribbons', 'icon' => 'video-camera'],
                ['name' => '标签', 'url' => '/home/tags', 'icon' => 'video-camera'],
                ['name' => '菜单', 'url' => '/home/menus', 'icon' => 'video-camera'],
            ]],
            ['name' => '工具类', 'url' => '', 'icon' => 'video-camera', 'children' => [
                ['name' => '间距', 'url' => '/home/spacing', 'icon' => 'video-camera'],
                ['name' => '色彩', 'url' => '/home/colors', 'icon' => 'video-camera'],
            ]],
            ['name' => '盒子', 'url' => '', 'icon' => 'video-camera', 'children' => [
                ['name' => '面板盒子', 'url' => '/home/panel-boxs', 'icon' => 'video-camera'],
                ['name' => '文字盒子', 'url' => '/home/title-boxs', 'icon' => 'video-camera'],
                ['name' => '内容盒子', 'url' => '/home/content-boxs', 'icon' => 'video-camera'],
            ]],
            ['name' => '表格', 'url' => '', 'icon' => 'video-camera', 'children' => [
                ['name' => '表格', 'url' => '/home/table', 'icon' => 'video-camera'],
            ]],
            ['name' => '表单', 'url' => '', 'icon' => 'video-camera', 'children' => [
                ['name' => '表单', 'url' => '/home/form', 'icon' => 'video-camera'],
            ]],
            ['name' => '组件', 'url' => '', 'icon' => 'video-camera', 'children' => [
                ['name' => '日历', 'url' => '/home/calendar', 'icon' => 'video-camera'],
                ['name' => '加载', 'url' => '/home/loading', 'icon' => 'video-camera'],
                ['name' => '弹出框&文字提示', 'url' => '/home/popovers&tooltips', 'icon' => 'video-camera'],
                ['name' => '滚动条', 'url' => '/home/scollbar', 'icon' => 'video-camera'],
                ['name' => 'Banner', 'url' => '/home/banner', 'icon' => 'video-camera'],
                ['name' => '图片', 'url' => '/home/images', 'icon' => 'video-camera'],
                ['name' => '时间轴', 'url' => '/home/timeline', 'icon' => 'video-camera'],
                ['name' => '可视化', 'url' => '/home/build', 'icon' => 'video-camera'],
                ['name' => 'icon 选择器', 'url' => '/home/icon-picker', 'icon' => 'video-camera'],
            ]],
            ['name' => '管理系统', 'url' => '', 'icon' => 'video-camera', 'children' => [
                ['name' => '配置', 'url' => '/home/administrator/config', 'icon' => 'video-camera'],
                ['name' => '管理员', 'url' => '/home/administrator/access/admin', 'icon' => 'video-camera'],
                ['name' => '菜单', 'url' => '/home/administrator/access/menus', 'icon' => 'video-camera'],
                ['name' => '节点', 'url' => '/home/administrator/access/nodes', 'icon' => 'video-camera'],
                ['name' => '角色', 'url' => '/home/administrator/access/roles', 'icon' => 'video-camera'],
            ]],
        ];

        // 循环的去遍历添加
        foreach ($menus as $menu) {
            // 创建一个账户
            $parentId = $this->add($menu);

            // 如果有子级，则继续新增子级
            if (UtilityLibrary::isArray($menu['children'])) {
                foreach ($menu['children'] as $childrenMenu) {
                    $this->add(array_merge($childrenMenu, ['parent_id' => $parentId]));
                }
            }
        }

        echo "Finished: " . __CLASS__ . " \r\n";
    }

    /**
     * 添加数据
     *
     * @param $menu
     *
     * @return int
     */
    protected function add($menu)
    {
        $model = \Yangyifan\Administrator\Access\Model\Menus\Menus::create([
            'name'      => $menu['name'],
            'url'       => $menu['url'],
            'status'    => 1,
            'parent_id' => $menu['parent_id'] ?? 0,
            'sort'      => 65525,
            'icon'      => $menu['icon'],
        ]);

        return $model->id;
    }
}
